<?php
/**
 * @author fyl
 * Date: 16-3-29 下午2:59
 */

class AdminService extends BaseService
{
    private static $self           =   NULL;
    private $mAdmin                 =   NULL;
    private $mLog                   =   NULL;
    private $mMail                   =   NULL;
    private $mInvestRecord          =   NULL;
    private $mRechargeRecord        =   NULL;
    private $mUser                  =   NULL;
    private $mNews                  =   NULL;
    private $mCompany               =   NULL;



    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {
        $this->mAdmin                  =   new Involved_AdminModel();
        $this->mLog                    =   new Involved_LogModel();
        $this->mMail                    =   new Involved_MailModel();
        $this->mInvestRecord           =   new Involved_InvestRecordModel();
        $this->mRechargeRecord         =   new Involved_RechargeRecordModel();
        $this->mUser                   =   new Involved_UserModel();
        $this->mNews                   =   new Involved_NewsModel();
        $this->mCompany                =   new Involved_CompanyModel();
    }
    /**
     * 验证用户
     */
    public function LoginValidator($paramObj)
    {
        if(empty($paramObj->username) || empty($paramObj->password)){
           return ;
        }
        if(strtolower($paramObj->captcha) != strtolower(Session::get('mt_Captcha'))){
            return ;
        }
        #数据验证
        $info = $this->mAdmin->PhoneAdminInfo($paramObj->username);

        $psswd=UserEnum::get(md5(md5($paramObj->password)),$info->salt);//加密方式

        if($info->password!=$psswd){
            return ;
        }
        $admin_info = $this->mAdmin->getLoginAdminInfo($paramObj->username,$psswd);
        if(empty($admin_info)){
            return ;
        }
        #存储SESSION
        Session::put(UserEnum::ADMIN_USER_KEY_ID, $admin_info->user_id);
        Session::put(UserEnum::ADMIN_USER_KEY, $admin_info);
        return $admin_info;
    }

    /**
     * 管理员列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function ManageAdmin()
    {
        return $this->mAdmin->ManageAdmin();

    }
    /**
     * 管理员添加
     * @param $paramObj
     * @return int
     * @throws Exception
     */
    public function AdminAdd($paramObj)
    {
        if(empty($paramObj->password))
            return Redirect::to('/admin/add_admin');
        if($paramObj->password!=$paramObj->sure){
            return Redirect::to('/admin/add_admin');
        }
        $info = $this->mAdmin->PhoneAdminInfo($paramObj->telephone);
        if(!empty($info)){
            echo "<script>alert('管理员名字已经存在，请重新添加');location='/admin/add_admin';</script>";return ;
        }
        if(!empty($paramObj->avatar))
            $paramObj->avatar =  QiniuUpload::instance()->upload_files($paramObj->avatar);
        $salts  =  rand(100000, 999999);
        $psswd=UserEnum::get(md5(md5($paramObj->password)),$salts);//加密方式
        $aData = [
            'name'              => $paramObj->name,
            'user_nick'         => $paramObj->user_nick,
            'telephone'         => $paramObj->telephone,
            'avatar'            => $paramObj->avatar,
            'password'          => $psswd,
            'role_id'           => $paramObj->role_id,
            'status'            => $paramObj->status,
            'salt'              => $salts,
            'add_time'           => time()
        ];
      $data= $this->mAdmin->insert($aData);
        if(!empty($data)){
        //return Redirect::to('/admin/manage_admin');
            echo "<script>alert('添加成功');location='/admin/manage_admin';</script>";return ;
        }
    }
    /**
     * 管理员编辑
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function GetAdminInfo($user_id)
    {
        return $this->mAdmin->GetAdminInfo($user_id);
    }

    /**
     *修改账户管理员
     */
    public function AdminUp($paramObj)
    {
        #图片上传
        $paramObj->avatar = !empty($paramObj->avatar) ?
            $paramObj->avatar =  QiniuUpload::instance()->upload_files($paramObj->avatar):$paramObj->avatar_old;
        #创建数组
        $aData = array(
            'name'            => $paramObj->name,
            'user_nick'      => $paramObj->user_nick,
            'telephone'      => $paramObj->telephone,
            'avatar'         => $paramObj->avatar,
            'role_id'        => $paramObj->role_id,
            'status'         => $paramObj->status,
            'add_time'       => time()
        );
       $date=$this->mAdmin->update($aData,['user_id'=>$paramObj->user_id]);
        if(!empty($date)){
            echo "<script>alert('修改成功');location='/admin/manage_admin';</script>";return ;
        }
    }
    /**
     * 管理员删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function DeleteAdmin($aData)
    {
        return $this->mAdmin->delete(['user_id'=>$aData->id]);
    }
    /**
     * 管理员修改密码
     */
    public function UpDatePwd($aData){
        if($aData->pwd!=$aData->sure){
            $mess['code']   = '2';
            $mess['mess']   = '修改密码两次不一致,请重新输入';
            return $mess;
        }
        $info = AdminService::instance()->GetAdminInfo($aData->user_id);
        $psswd=UserEnum::get(md5(md5($aData->pwd)),$info->salt);//加密方式
        if($info->password==$psswd){
            $mess['code']   = '2';
            $mess['mess']   = '新密码不能和原密码一样';
            return $mess;
        }
        #创建数组
        $data = array(
            'password'        => $psswd
        );
        $data=$this->mAdmin->update($data,['user_id'=>$aData->user_id]);
        if(empty( $data)){
            $mess['code']   = '2';
            $mess['mess']   = '修改密码失败';
            return $mess;
        }else{
            $mess['code']   = '1';
            $mess['mess']   = '修改密码成功';
            $mess['url']    = "/admin/manage_admin";
            return $mess;
        }
    }

    /**
     * 日志列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function LogService()
    {
        return $this->mLog->LogService();
    }
    /**
     * 日志删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function DeleteLog($aData)
    {
        return $this->mLog->delete(['log_id'=>$aData->logid]);
    }
    /**
     * 获得默认登录页
     * @param $role_id
     * @return string
     */
    public function getDefaultPage($role_id)
    {
        switch($role_id)
        {
            case AdminEnum::SUPPER_ADMIN_ROLE:
                //管理
                $page =  '/admin_index';
                break;
            case AdminEnum::GENERAL_ADMIN_ROLE:
                //管理
                $page =  '/admin_index';
                break;
            case AdminEnum::EDITOR_ROLE:
                //管理
                $page =  '/admin_index';
                break;
            default:
                //返回登录
                $page =  '/admin_index';
                break;
        }
        return $page;
    }

    /**
     * 站内信
     * @param $role_id
     * @return string
     */
    public function MessageMail(){
        $uid=session_get(UserEnum::ADMIN_USER_KEY_ID);
        return $this->mMail->MessageMail($uid);
    }

    /**
     * 站内信
     * @param $role_id
     * @return string
     */
    public function AdminMessage($uid){
        return $this->mAdmin->AdminMessage($uid);
    }
    /**
     * 站内信
     * @param $role_id
     * @return string
     */
    public function AdminRole($roid){
        $uid=session_get(UserEnum::ADMIN_USER_KEY_ID);
        return $this->mAdmin->AdminRole($roid,$uid);
    }
    /**
     * 站内信发送
     * @param $role_id
     * @return string
     */
    public function AddMessage($aData){
        $role = json_encode($aData);
        $aData = json_decode($role, true);
        if(empty($aData['title'])||empty($aData['cate_name'])){
            $mess['code']   = '2';
            $mess['mess']   = '参数错误';
            return $mess;
        }
        $uid=session_get(UserEnum::ADMIN_USER_KEY_ID);
        $arrays=explode(",",$aData['cate_name']);
        for($i=0;$i<count($arrays);$i++){
            $cart_id = $arrays["$i"];
            $aData = array(
                'user_id'               => $uid,//发送者
                'title'                 => $aData['title'],
                'content'               => $aData['content'],
                'admin_id'              => $cart_id,//发送者
                'statue'                => 0,
                'add_time'              => time()
            );
           $deat=$this->mMail->insert($aData);
        }
        return 1;

    }
    /**
     * 站内信详情页
     * @param $role_id
     * @return string
     */
    public function MailContent($mail_id){
       return $this->mMail->MailContent($mail_id);

    }
    /**
     * 站内信状态修改
     * @param $role_id
     * @return string
     */
    public function MailFirst($mail_id){
        $aData = array(
            'statue'                => 1
        );
        return $this->mMail->update($aData,['mail_id'=>$mail_id]);
    }

    /**
     * 站内信统计显示
     * @param $role_id
     * @return string
     */
    public function MailCount(){
        $uid=session_get(UserEnum::ADMIN_USER_KEY_ID);
        return $this->mMail->MailCount($uid);
    }



}